package br.com.sigeden.dao;

import br.com.sigeden.model.Condicao;
import br.com.sigeden.util.HibernateUtil;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author David Amom <davidamom@gmail.com>
 */
public class CondicaoDAO {

    private Session sessao;
    private Transaction transacao;
    private List<Condicao> condicoes;

    public List<Condicao> listarCondicoes() {

        sessao = HibernateUtil.getSessionFactory().openSession();

        Criteria criteria = sessao.createCriteria(Condicao.class);
        this.condicoes = criteria.list();
        
        sessao.close();
        return condicoes;

    }

    public void adicionarCondicao(Condicao condicao) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            transacao = sessao.beginTransaction();

            Condicao c = new Condicao();
            c.setNome(condicao.getNome());

            sessao.save(c);
            transacao.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sessao.close();
        }
    }

    public void removerCondicao(Condicao condicao) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            transacao = sessao.beginTransaction();

            sessao.delete(condicao);

            transacao.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sessao.close();
        }
    }

    public void atualizarCondicao(Condicao condicao) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            transacao = sessao.beginTransaction();

            sessao.update(condicao);

            transacao.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sessao.close();
        }
    }
}
